/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package View;

import Controller.PrescricaoController;
import Model.Prescricao;
import javax.swing.JOptionPane;
    
public class PrescricaoView extends javax.swing.JDialog {
    //variável para diferenciar alteração de inclusão. Inclusão = False, Alteração = True
    boolean alterar;
    int codigo;
    PrescricaoController pControl = new PrescricaoController();
    
    public PrescricaoView(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
        setLocationRelativeTo( null );
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        pnPrescricaoCampo = new javax.swing.JPanel();
        tfPrescricaoNome = new javax.swing.JTextField();
        lbPrescricaoNome = new javax.swing.JLabel();
        lbPrescricaoCaracteristica = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        taPrescricaoCaracteristica = new javax.swing.JTextArea();
        pnPrescricaoBotao = new javax.swing.JPanel();
        btPrescricaoSalvar = new javax.swing.JButton();
        btPrescricaoCancelar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Prescrição");

        pnPrescricaoCampo.setBorder(javax.swing.BorderFactory.createTitledBorder("Dados de Prescrição"));

        tfPrescricaoNome.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                tfPrescricaoNomeKeyReleased(evt);
            }
        });

        lbPrescricaoNome.setText("Nome:");

        lbPrescricaoCaracteristica.setText("Característica:");

        taPrescricaoCaracteristica.setColumns(20);
        taPrescricaoCaracteristica.setRows(5);
        taPrescricaoCaracteristica.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyReleased(java.awt.event.KeyEvent evt) {
                taPrescricaoCaracteristicaKeyReleased(evt);
            }
        });
        jScrollPane1.setViewportView(taPrescricaoCaracteristica);

        javax.swing.GroupLayout pnPrescricaoCampoLayout = new javax.swing.GroupLayout(pnPrescricaoCampo);
        pnPrescricaoCampo.setLayout(pnPrescricaoCampoLayout);
        pnPrescricaoCampoLayout.setHorizontalGroup(
            pnPrescricaoCampoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, pnPrescricaoCampoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnPrescricaoCampoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(lbPrescricaoNome, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addGroup(pnPrescricaoCampoLayout.createSequentialGroup()
                        .addComponent(lbPrescricaoCaracteristica)
                        .addGap(0, 24, Short.MAX_VALUE)))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnPrescricaoCampoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(tfPrescricaoNome, javax.swing.GroupLayout.PREFERRED_SIZE, 250, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jScrollPane1, javax.swing.GroupLayout.Alignment.TRAILING))
                .addContainerGap())
        );
        pnPrescricaoCampoLayout.setVerticalGroup(
            pnPrescricaoCampoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnPrescricaoCampoLayout.createSequentialGroup()
                .addGroup(pnPrescricaoCampoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(tfPrescricaoNome, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(lbPrescricaoNome))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(pnPrescricaoCampoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(pnPrescricaoCampoLayout.createSequentialGroup()
                        .addComponent(lbPrescricaoCaracteristica)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(pnPrescricaoCampoLayout.createSequentialGroup()
                        .addComponent(jScrollPane1)
                        .addGap(3, 3, 3))))
        );

        pnPrescricaoBotao.setBorder(javax.swing.BorderFactory.createTitledBorder(""));

        btPrescricaoSalvar.setText("Salvar");
        btPrescricaoSalvar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPrescricaoSalvarActionPerformed(evt);
            }
        });

        btPrescricaoCancelar.setText("Cancelar");
        btPrescricaoCancelar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btPrescricaoCancelarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout pnPrescricaoBotaoLayout = new javax.swing.GroupLayout(pnPrescricaoBotao);
        pnPrescricaoBotao.setLayout(pnPrescricaoBotaoLayout);
        pnPrescricaoBotaoLayout.setHorizontalGroup(
            pnPrescricaoBotaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnPrescricaoBotaoLayout.createSequentialGroup()
                .addGap(89, 89, 89)
                .addComponent(btPrescricaoSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(18, 18, 18)
                .addComponent(btPrescricaoCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 90, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(89, Short.MAX_VALUE))
        );
        pnPrescricaoBotaoLayout.setVerticalGroup(
            pnPrescricaoBotaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(pnPrescricaoBotaoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(pnPrescricaoBotaoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btPrescricaoSalvar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(btPrescricaoCancelar, javax.swing.GroupLayout.PREFERRED_SIZE, 30, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addComponent(pnPrescricaoCampo, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                    .addComponent(pnPrescricaoBotao, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
                .addContainerGap())
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(pnPrescricaoCampo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(pnPrescricaoBotao, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void btPrescricaoCancelarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPrescricaoCancelarActionPerformed
        this.dispose();
    }//GEN-LAST:event_btPrescricaoCancelarActionPerformed

    public void receberDadosPrescricao(Prescricao p){
        tfPrescricaoNome.setText(p.getNome());
        taPrescricaoCaracteristica.setText(p.getCaracteristica());
    }
    
    private void btPrescricaoSalvarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btPrescricaoSalvarActionPerformed
        if(tfPrescricaoNome.getText().equals("")){
            JOptionPane.showMessageDialog(null, "Preencha os campos corretamente!");
        }
        else{
            Prescricao p = new Prescricao();
            p.setNome(tfPrescricaoNome.getText());
            p.setCaracteristica(taPrescricaoCaracteristica.getText());
            if(alterar == false){
                pControl.incluirPrescricao(p);
                tfPrescricaoNome.setText("");
                taPrescricaoCaracteristica.setText("");
                tfPrescricaoNome.requestFocus();
            }
            else{
                p.setCodigo(codigo);
                pControl.alterarPrescricao(p);
            }
        }
    }//GEN-LAST:event_btPrescricaoSalvarActionPerformed

    private void tfPrescricaoNomeKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_tfPrescricaoNomeKeyReleased
        tfPrescricaoNome.setText(tfPrescricaoNome.getText().toUpperCase());
    }//GEN-LAST:event_tfPrescricaoNomeKeyReleased

    private void taPrescricaoCaracteristicaKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_taPrescricaoCaracteristicaKeyReleased
        taPrescricaoCaracteristica.setText(taPrescricaoCaracteristica.getText().toUpperCase());
    }//GEN-LAST:event_taPrescricaoCaracteristicaKeyReleased

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        /* Set the Nimbus look and feel */
        //<editor-fold defaultstate="collapsed" desc=" Look and feel setting code (optional) ">
        /* If Nimbus (introduced in Java SE 6) is not available, stay with the default look and feel.
         * For details see http://download.oracle.com/javase/tutorial/uiswing/lookandfeel/plaf.html 
         */
        try {
            for (javax.swing.UIManager.LookAndFeelInfo info : javax.swing.UIManager.getInstalledLookAndFeels()) {
                if ("Nimbus".equals(info.getName())) {
                    javax.swing.UIManager.setLookAndFeel(info.getClassName());
                    break;
                }
            }
        } catch (ClassNotFoundException ex) {
            java.util.logging.Logger.getLogger(PrescricaoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (InstantiationException ex) {
            java.util.logging.Logger.getLogger(PrescricaoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (IllegalAccessException ex) {
            java.util.logging.Logger.getLogger(PrescricaoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        } catch (javax.swing.UnsupportedLookAndFeelException ex) {
            java.util.logging.Logger.getLogger(PrescricaoView.class.getName()).log(java.util.logging.Level.SEVERE, null, ex);
        }
        //</editor-fold>

        /* Create and display the dialog */
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                PrescricaoView dialog = new PrescricaoView(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btPrescricaoCancelar;
    private javax.swing.JButton btPrescricaoSalvar;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JLabel lbPrescricaoCaracteristica;
    private javax.swing.JLabel lbPrescricaoNome;
    private javax.swing.JPanel pnPrescricaoBotao;
    private javax.swing.JPanel pnPrescricaoCampo;
    private javax.swing.JTextArea taPrescricaoCaracteristica;
    private javax.swing.JTextField tfPrescricaoNome;
    // End of variables declaration//GEN-END:variables
}
